/********************************************************************************
* File Name:	mainwindow.cpp
* Description:	第3章 实例5：ListView的实例
* Reference book:《Linux环境下Qt4图形界面与MySQL编程》，机械工业出版社.2012.1
* E_mail: openlinux2011@gmail.com
*
********************************************************************************/
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QBrush>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    standardItemModel = new QStandardItemModel(this);
    QStringList strList;
    strList.append("a");
    strList.append("b");
    strList.append("c");
    strList.append("d");
    strList.append("e");
    strList.append("f");
    strList.append("g");
    strList << "h";
    strList += "i";
    int nCount = strList.size();
    for(int i = 0; i < nCount; i++)
    {
        QString string = static_cast<QString>(strList.at(i));
        QStandardItem *item = new QStandardItem(string);
        if(i % 2 == 1)
        {
            QLinearGradient linearGrad(QPointF(0, 0), QPointF(200, 200));
            linearGrad.setColorAt(0, Qt::darkGreen);
            linearGrad.setColorAt(1, Qt::yellow);
            QBrush brush(linearGrad);
            item->setBackground(brush);
        }
        standardItemModel->appendRow(item);
    }
    ui->listView->setModel(standardItemModel);
    connect(ui->listView,SIGNAL(clicked(QModelIndex)),this,SLOT(itemClicked(QModelIndex)));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::itemClicked(QModelIndex index)
{
    ui->textEdit->setText(ui->textEdit->toPlainText()+index.data().toString()+"\n");
}
